package common

import (
	"organoid_public/domain/common/service"
	"organoid_public/global"
	eventbus "organoid_public/infrastructure/event"
)

type useCase struct {
	sub eventbus.Subscribe
	svc service.Service
}

func initCommonUseCase(svc service.Service, sub eventbus.Subscribe) *useCase {
	return &useCase{svc: svc, sub: sub}
}

func wireService() (eventbus.Subscribe, service.Service, error) {
	defaultDb := global.GormDb.GetDb()
	log := global.Log
	uc, cleanup, err := wireApp(defaultDb, global.EventManager, log)
	if err != nil {
		log.Error("module", "common", "func", "wireService", "error", err.Error())
		return nil, nil, err
	}
	defer cleanup()

	return uc.sub, uc.svc, err
}

func EventCommonSubscribe() (eventbus.Subscribe, error) {
	es, _, err := wireService()
	if err != nil {
		return nil, err
	}

	return es, nil
}

// Service 获取领域服务实体
func Service() (service.Service, error) {
	_, svc, err := wireService()
	return svc, err
}
